<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>Document</title>
    <style></style>
  </head>
  <body>
    <div>
      <h1>生成器Genetor的应用</h1>
      <p>1.最重要的目的是解决异步编程中过渡嵌套的问题</p>
    </div>
    <script>
      //发号器
      function* createIdMaker() {
        let id = 1;
        while (true) {
          yield id++;
        }
      }
      const idMaker = createIdMaker();
      console.log(idMaker.next()); //{value: 1, done: false}
      console.log(idMaker.next()); //{value: 2, done: false}
      console.log(idMaker.next()); //{value: 3, done: false}
      console.log(idMaker.next()); //{value: 4, done: false}
      console.log(idMaker.next()); //{value: 5, done: false}

      const todos = {
        lift: ["吃饭", "睡觉", "打豆豆"],
        learn: ["语文", "数学", "英语"],
        work: ["喝茶"],
        [Symbol.iterator]: function* () {
          const all = [...this.lift, ...this.learn, ...this.work];
          for (const item of all) {
            yield item;
          }
        },
      };
      for (const item of todos) {
        console.log(item);
      }
    </script>
  </body>
</html>
